package com.qiaofang.controller;

import com.qiaofang.common.response.CommonResponse;
import com.qiaofang.common.response.SuccessfulResponseFactory;
import com.qiaofang.constant.CompanyTypeEnum;
import com.qiaofang.dto.ListPartnerInfoDTO;
import com.qiaofang.dto.PartnerInfoDTO;
import com.qiaofang.request.*;
import com.qiaofang.response.GetPartnerInfoResponse;
import com.qiaofang.response.ListOptionCompanyResponse;
import com.qiaofang.response.ListPartnerInfoResponse;
import com.qiaofang.service.MerchantCompanyService;
import com.qiaofang.service.PartnerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@Api(tags = {"商户人员接口"})
@RequestMapping(value = "/merchantPartnerInfo", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public class MerchantPartnerInfoController {

    @Autowired
    private PartnerInfoService partnerInfoService;

    @PostMapping(value = "/listPartnerInfo")
    @ApiOperation(value = "查询列表")
    public ListPartnerInfoResponse listPartnerInfo(@RequestBody ListPartnerInfoRequest request) {
        ListPartnerInfoResponse response = new ListPartnerInfoResponse();
        ListPartnerInfoDTO dto = new ListPartnerInfoDTO();
        dto.setStatus(request.getStatus());
        dto.setAccountType(request.getAccountType());
        dto.setCompanyId(request.getCompanyId());
        dto.setCreateTimeStart(request.getCreateTimeStart());
        dto.setCreateTimeEnd(request.getCreateTimeEnd());
        dto.setSearchContent(request.getSearchContent());
        dto.setCompanyType(CompanyTypeEnum.Developer.getType());
        response.setData(partnerInfoService.listPartnerInfo(dto, request.getPage()));
        return response;
    }

    @PostMapping(value = "/addPartnerInfo")
    @ApiOperation(value = "新增")
    public CommonResponse addPartnerInfo(@RequestBody @Valid AddPartnerInfoRequest request) {
        PartnerInfoDTO dto = new PartnerInfoDTO();
        dto.setAccountName(request.getAccountName());
        dto.setTelephone(request.getTelephone());
        dto.setStatus(request.getStatus());
        dto.setResetPassword(request.getResetPassword());
        dto.setCompanyId(request.getCompanyId());
        dto.setCompanyType(CompanyTypeEnum.Developer.getType());
        dto.setAccountType(request.getAccountType());
        partnerInfoService.addPartnerInfo(dto);
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/updatePartnerInfo")
    @ApiOperation(value = "编辑")
    public CommonResponse updatePartnerInfo(@RequestBody @Valid UpdatePartnerInfoRequest request) {
        PartnerInfoDTO dto = new PartnerInfoDTO();
        dto.setId(request.getId());
        dto.setAccountName(request.getAccountName());
        dto.setTelephone(request.getTelephone());
        dto.setStatus(request.getStatus());
        dto.setResetPassword(request.getResetPassword());
        dto.setCompanyId(request.getCompanyId());
        dto.setCompanyType(CompanyTypeEnum.Developer.getType());
        dto.setAccountType(request.getAccountType());
        partnerInfoService.updatePartnerInfo(dto);
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/getPartnerInfo")
    @ApiOperation(value = "查询详情")
    public GetPartnerInfoResponse getPartnerInfo(@RequestBody @Valid GetPartnerInfoRequest request) {
        GetPartnerInfoResponse response = new GetPartnerInfoResponse();
        response.setData(partnerInfoService.getPartnerInfo(request.getId()));
        return response;
    }

    @PostMapping(value = "/enablePartnerInfo")
    @ApiOperation(value = "启用")
    public CommonResponse enablePartnerInfo(@RequestBody @Valid EnablePartnerInfoRequest request) {
        partnerInfoService.enablePartnerInfo(request.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/disablePartnerInfo")
    @ApiOperation(value = "禁用")
    public CommonResponse disablePartnerInfo(@RequestBody @Valid DisablePartnerInfoRequest request) {
        partnerInfoService.disablePartnerInfo(request.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/deletePartnerInfo")
    @ApiOperation(value = "删除")
    public CommonResponse deletePartnerInfo(@RequestBody @Valid DeletePartnerInfoRequest request) {
        partnerInfoService.deletePartnerInfo(request.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/installAdminPartnerInfo")
    @ApiOperation(value = "设置超管")
    public CommonResponse installAdminPartnerInfo(@RequestBody @Valid InstallAdminPartnerInfoRequest request) {
        partnerInfoService.installAdminPartnerInfo(request.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/cancelAdminPartnerInfo")
    @ApiOperation(value = "取消超管")
    public CommonResponse cancelAdminPartnerInfo(@RequestBody @Valid CancelAdminPartnerInfoRequest request) {
        partnerInfoService.cancelAdminPartnerInfo(request.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/batchEnablePartnerInfo")
    @ApiOperation(value = "批量启用")
    public CommonResponse batchEnablePartnerInfo(@RequestBody @Valid BatchEnablePartnerInfoRequest request) {
        partnerInfoService.batchEnablePartnerInfo(request.getIds());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/batchDisablePartnerInfo")
    @ApiOperation(value = "批量禁用")
    public CommonResponse batchDisablePartnerInfo(@RequestBody @Valid BatchDisablePartnerInfoRequest request) {
        partnerInfoService.batchDisablePartnerInfo(request.getIds());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/batchDeletePartnerInfo")
    @ApiOperation(value = "批量删除")
    public CommonResponse batchDeletePartnerInfo(@RequestBody @Valid BatchDeletePartnerInfoRequest request) {
        partnerInfoService.batchDeletePartnerInfo(request.getIds());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/cleanPartnerCache")
    @ApiOperation(value = "清理用户缓存")
    public CommonResponse cleanPartnerCache(@RequestBody @Valid GenericIdRequest request) {
        partnerInfoService.cleanPartnerCache(request.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

}
